package com.cius.ssm.controller;


import com.cius.ssm.entity.Product;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现controller接口的处理器
 * Created by ciuscheung on 17/4/4.
 */
public class ProductController1 implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //调用service查找数据库,查询商品列表,这里使用静态模拟

        List<Product> lists = new ArrayList<Product>();

        Product product1 = new Product();
        product1.setId(1);
        product1.setName("手机");

        Product product2 = new Product();
        product2.setId(2);
        product2.setName("电脑");

        lists.add(product1);
        lists.add(product2);

//        返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
//        相当于request的setAttribute方法,在jsp页面中通过lists取数据
        modelAndView.addObject("lists",lists);
        modelAndView.setViewName("/WEB-INF/jsp/product/productList.jsp");

        return modelAndView;
    }
}
